home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / PowerPlant / LButtonKeyAtt / LButtonKeyAtt.cp < prev    next >
Encoding:
Text File  |  1995-09-24  |  3.3 KB  |  139 lines  |  [TEXT/CWIE]

  1. #ifdef PowerPlant_PCH
  2. #include PowerPlant_PCH
  3. #endif
  4.  
  5.  
  6. #include "LButtonKeyAtt.h"
  7.  
  8.  
  9. LButtonKeyAtt    *LButtonKeyAtt::CreateFrom( LStream *inStream)
  10. {
  11.     return new LButtonKeyAtt( inStream);
  12. }
  13.  
  14.  
  15. LButtonKeyAtt::LButtonKeyAtt()
  16.  : LAttachment( msg_KeyPress, true), fKeyHandler( NULL), fKeyChar( '\0'), fHintVis( false)
  17. {
  18. }
  19.  
  20.  
  21. LButtonKeyAtt::LButtonKeyAtt( LStream *inStream)
  22.  : fKeyHandler( NULL), fKeyChar( '\0'), fHintVis( false)
  23. {
  24. }
  25.  
  26.  
  27. LButtonKeyAtt::~LButtonKeyAtt()
  28. /* Remove ourselves from our keystroke-handling host. */
  29. {
  30.     if ( fKeyHandler)
  31.         fKeyHandler->RemoveAttachment( this);
  32. }
  33.  
  34.  
  35. void            LButtonKeyAtt::SetOwnerHost( LAttachable *inOwnerHost)
  36. /* Locate the commander that will receive our keystrokes. */
  37. /* Also, determine which character should be used for pressing the button. */
  38. {
  39. LStdControl        *pOwner;
  40. LView                *pView;
  41. Str255            desc;
  42.  
  43.     LAttachment::SetOwnerHost( inOwnerHost);
  44.     if ( pOwner = dynamic_cast<LStdControl*>( inOwnerHost)) {
  45.         for ( pView = pOwner->GetSuperView(); pView; pView = pView->GetSuperView()) {
  46.             if ( fKeyHandler = dynamic_cast<LCommander*>( pView))
  47.                 break;
  48.         }
  49.  
  50.         if ( fKeyHandler) {
  51.             fKeyHandler->AddAttachment( this, NULL, false);
  52.             this->StartRepeating();    // start looking for cmd-key down
  53.         }
  54.  
  55.         pOwner->GetDescriptor( desc);
  56.         LowerText( (char*) &desc[ 1], desc[ 0]);
  57.         fKeyChar = desc[ 1];        // should use CharByte() ?
  58.         
  59.     } else if ( inOwnerHost == NULL) {        // we're being removed
  60.         if ( fKeyHandler) {
  61.             this->StopRepeating();    // stop looking for cmd-key down
  62.             fKeyHandler->RemoveAttachment( this);
  63.             fKeyHandler = NULL;
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69. void        LButtonKeyAtt::SpendTime( const EventRecord &inMacEvent)
  70. // If the command key is down, display the hint. If it's not, erase it.
  71. {
  72. LStdControl        *pOwner;
  73. Boolean            cmdDown;
  74.  
  75.     if ( ( pOwner = dynamic_cast<LStdControl*>( mOwnerHost)) && pOwner->IsEnabled()) {
  76.         if ( ( cmdDown = ( inMacEvent.modifiers & cmdKey) != 0) &&
  77.               !fHintVis)
  78.         {
  79.             this->SetHint( true);
  80.         } else if ( !cmdDown && fHintVis) {
  81.             this->SetHint( false);
  82.         }
  83.     }
  84. }
  85.  
  86.  
  87. void        LButtonKeyAtt::ExecuteSelf( MessageT inMessage, void *ioParam)
  88. // Intercept fKeyHandler's keystroke events.
  89. {
  90. EventRecord        *pKeyEvt = (EventRecord*) ioParam;
  91. LStdControl        *pOwner;
  92.  
  93.     mExecuteHost = true;
  94.     if ( ( pKeyEvt->modifiers & cmdKey) && fKeyChar == ( pKeyEvt->message & 0xFF)) {
  95.         if ( ( pOwner = dynamic_cast<LStdControl*>( mOwnerHost)) && pOwner->IsEnabled()) {
  96.             pOwner->SimulateHotSpotClick( kControlButtonPart);
  97.             mExecuteHost = false;
  98.         }
  99.     }
  100. }
  101.  
  102.  
  103. void        LButtonKeyAtt::GetHintPos( const Rect& localPaneR, Point *pPos)
  104. // Determine where to put hint relative to button. Hard-coded; should be improved at some point.
  105. {
  106.     pPos->h = localPaneR.left - 24;
  107.     pPos->v = localPaneR.bottom - 6;
  108. }
  109.  
  110.  
  111. void        LButtonKeyAtt::SetHint( Boolean visible)
  112. // Either display the hint next to the button, or hide it.
  113. {
  114. LStdControl        *pOwner;
  115. Rect                btnR;
  116. Point                pos;
  117. Byte                str[2] = { char_Propeller, ' ' };
  118.  
  119.     if ( pOwner = dynamic_cast<LStdControl*>( mOwnerHost)) {
  120.         if ( pOwner->FocusDraw() && pOwner->CalcLocalFrameRect( btnR))
  121.         {
  122.             TextFont( systemFont);
  123.             TextSize( 12);
  124.             TextFace( 0);
  125.             TextMode( visible ? srcCopy : srcBic);
  126.             this->GetHintPos( btnR, &pos);
  127.             MoveTo( pos.h, pos.v);
  128.             str[ 1] = fKeyChar;
  129.             UpperText( (char*) &str[ 1], 1);
  130.             DrawText( str, 0, sizeof str);
  131.             TextMode( srcCopy);
  132.         }
  133.     }
  134.     fHintVis = visible;
  135. }
  136.  
  137.  
  138.  
  139.